<template>
  <div ref="vantaRef" style="width: 100%;height: 100vh;" class="wrapper">
    <div style="margin: 200px auto;background-color: #FFFFFF;width: 350px;height: 300px;padding: 20px;border-radius: 10px;">
      <div style="margin: 20px 0;text-align: center;font-size: 24px;"><b>登 录</b></div>
      <el-form :model="userForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input size="medium" v-model="userForm.username" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" v-model="userForm.password" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <div style="margin: 10px 0;text-align: right">
          <el-form-item>
            <el-button type="primary" size="small" @click="submitForm('ruleForm')">登录</el-button>
            <el-button type="warning" size="small" @click="$router.push('/register')">注册</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {setRoutes} from "@/router";
import * as THREE from 'three';
import BIRDS from 'vanta/src/vanta.birds'
import WAVES from 'vanta/src/vanta.waves'

export default {
  name: "Login",
  data() {
    return {
      userForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //发送登录请求
          this.request.post("/user/login",this.userForm).then( res => {
            if (res.code === "200") {
              localStorage.setItem("user",JSON.stringify(res.data))
              localStorage.setItem("menus",JSON.stringify(res.data.menus))
              //动态设置当前登录用户的路由
              setRoutes()
              //判断是否是学生登录
              if (res.data.role === "ROLE_STUDENT" || res.data.role === "ROLE_PATIENT") {
                this.$router.push("/front/home")
              }else {
                this.$router.push("/")
              }
              // this.$message.success("登录成功")
              this.$message({
                showClose: true,
                message: '登录成功',
                type: 'success',
                duration: 1000
              });
            }else {
              this.$message.error(res.msg)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  },
  mounted() {
    this.vantaEffect = WAVES({
      el: this.$refs.vantaRef,
      THREE
    })
    this.vantaEffect.setOptions({
      mouseControls: true,
      touchControls: true,
      gyroControls: false,
      minHeight: 200.00,
      minWidth: 200.00,
      skyColor: 0x91cde3,
      cloudColor: 0xc9c9d9,
      cloudShadowColor: 0x174b7d,
      sunColor: 0xe37f05,
      speed: 1.50
    })
  },
  beforeDestroy() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
  }
}
</script>

<style scoped>
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right,#FC4668,#3F5EF8);
  overflow: hidden;
}
</style>